package servlets;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lucendoc.IndexaDoc;
import lucenepdf.BuscaPdf;
import lucenepdf.Indexer;
import luceneppt.IndexaPpt;

/**
 * Servlet implementation class Busca
 */
public class Busca extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Busca() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String contenido = request.getParameter("txtBusca");
		String autor = request.getParameter("txtAutor");
		String stipo = request.getParameter("stipo");
		String fecha = request.getParameter("fecha");
        if (fecha.startsWith("0")){
        	fecha = fecha.substring(1,10);
        }
		
		//File arch = new File(this.getClass().getResource("./Busca.class").toString());
		//arch.delete();
		//File dir = new File("c:\\indexdir");
		//borrarDirectorio(dir);
		//dir.delete();
		
		//System.out.print(this.getClass().getResource("./Busca.class"));
		
		//System.out.println(getServletContext().getRealPath(arg0));
		
		if (stipo.equals(".pdf")){
			Indexer in = new Indexer();
			BuscaPdf resul = new BuscaPdf(contenido, autor, fecha);
			try{
				   request.setAttribute("resultado", resul.resultado());
				   getServletConfig().getServletContext().getRequestDispatcher("/resultados.jsp").forward(request, response);	
				}
				catch(Exception err){
				
				}
		}
		if (stipo.equals(".doc")){
			IndexaDoc in = new IndexaDoc();
			BuscaPdf resul = new BuscaPdf(contenido, autor, fecha);
			try{
				   request.setAttribute("resultado", resul.resultado());
				   getServletConfig().getServletContext().getRequestDispatcher("/resultados.jsp").forward(request, response);	
				}
				catch(Exception err){
				
				}
			
		}
		if (stipo.equals(".ppt")){
			IndexaPpt in = new IndexaPpt();
			BuscaPdf resul = new BuscaPdf(contenido, autor, fecha);
			try{
				   request.setAttribute("resultado", resul.resultado());
				   getServletConfig().getServletContext().getRequestDispatcher("/resultados.jsp").forward(request, response);	
				}
				catch(Exception err){
				
				}
			
		}
	}
	
	  private static void borrarDirectorio(File dir){
		  File[] ficheros = dir.listFiles();
		  for (int x=0;x<ficheros.length;x++){
			  if (ficheros[x].isDirectory()) {
				  borrarDirectorio(ficheros[x]);
				}
			  System.out.print("Borrando: "+ficheros[x]);
			  ficheros[x].delete();
		  }
	  }

}
